var storageCal={
	init:{
			timeElement:null,
			calDiv:null,
			calDivClose:function(){
				this.init.calDiv.style.display="none";
			},
			handles:[]
		},
	data:{},
	datePrice:{},
	strpad:function(str){
		str=String(str);
		if(str.length==1){
			return "0"+str;
		}else{
			return str;
		}
	},
	outputHtml:function(json,yearmonth,fromDate){
		var d={};
		for(r in json){
			d[r.date]=r.price;
		}		
		var nowtime=new Date();
			if(fromDate){
				
				fromDate=fromDate.split("-");
				fromDate=fromDate.join("/");
				if((new Date(fromDate)).getTime()>nowtime)
				 nowtime=(new Date(fromDate));
			}
			
			nowtime.setHours(0,0,0,0);
			
			nowtime=nowtime.getTime();//凌晨时间
			
			
		var begintime=yearmonth+"-01";
			begintime=begintime.split("-");
			begintime=begintime.join("/");
		var beginDate=new Date(begintime);
		var year=beginDate.getFullYear();
		var month=beginDate.getMonth()+1;
		var lastmonth=(month-1==0)?12:month-1;
		var nextmonth=(month+1==13)?1:month+1;
		var days=(new Date(beginDate.getFullYear(),beginDate.getMonth()+1,0)).getDate();
		var monthdays=days;
		var emptydays=beginDate.getDay();
		var endtime=yearmonth+"-"+days;
			endtime=endtime.split("-");
			endtime=endtime.join("/");
		var endDate=new Date(endtime);
		days+=beginDate.getDay()+(7-endDate.getDay());

		beginDate.setTime(beginDate.getTime()-(24*3600000*beginDate.getDay()));
		var lastmonth_none=(json.about.mintime*1000-beginDate.getTime()<0)?"":"lastmonth_none";
		var nextmonth_none=(json.about.maxtime*1000-endDate.getTime()>0)?"":"nextmonth_none";
		var html='<div class="storageCal" id="storageCalContent"><div class="monthbox">'+
					'<div class="title"><span class="lastmonth '+lastmonth_none+'"><a title="'+lastmonth+'月">&nbsp;</a></span><span class="nextmonth '+nextmonth_none+'"><a title="'+nextmonth+'月">&nbsp;</a></span><span class="year">'+year+'年'+month+'月  全选<input type="checkbox" id="checkAll"/></span><span class="close"></span><input type="hidden" class="yearmonth" value="'+yearmonth+'"/></div>'+
						'<table>'+
						 '<thead>'+
							'<tr>'+
								'<th class="weeken">周日 <input type="checkbox" class="weekSele" value="0"/></th>'+
								'<th>周一 <input type="checkbox" class="weekSele" value="1"/></th>'+
								'<th>周二 <input type="checkbox" class="weekSele" value="2"/></th>'+
								'<th>周三 <input type="checkbox" class="weekSele" value="3"/></th>'+
								'<th>周四 <input type="checkbox" class="weekSele" value="4"/></th>'+
								'<th>周五 <input type="checkbox" class="weekSele" value="5"/></th>'+
								'<th class="weeken">周六 <input type="checkbox" class="weekSele" value="6"/></th>'+
							'</tr></thead><tbody>';
		for(var i=0,j=0;i<days-1;i++){
			if(i%7==0){
				html+='<tr>';
			}
			var date=beginDate.getFullYear()+"-"+this.strpad((beginDate.getMonth()+1))+"-"+this.strpad(beginDate.getDate());
			var price="";
			var priceT="";
			var lprice="";
			var lpriceT="";
			var valid="";
			var validT="";
			var inputHtml="";
			var storage="";
			var storageT="";
			var sale="";
			var uid=0;
			var remain;
			if(json.pricelists[j]&&json.pricelists[j].date==date){
				this.datePrice[date]=json.pricelists[j].price;
				price=json.pricelists[j].sprice;
				//priceT=json.pricelists[j].price?"¥"+json.pricelists[j].price:"";
				priceT="供货：¥"+json.pricelists[j].sprice;
				lprice=json.pricelists[j].lprice
				lpriceT="零售：¥"+json.pricelists[j].lprice;
				storage=json.pricelists[j].storage;
				sale=json.pricelists[j].sale;
				storageT="库存："+sale+"/"+storage;
				uid=json.pricelists[j].uid||0
				if(storage==-1){
					storageT="库存：不限";
				}
				j++;
			}
			inputHtml='<input type="checkbox" name=""/>';
			if(i<emptydays||i>=monthdays+emptydays){
				html+='<td><div class="detail"></div></td>';
			}else if(beginDate.getTime()<nowtime){
				html+='<td><div date="'+date+'" class="detail"><span>'+beginDate.getDate()+'</span><div class="price"></div></div></td>';
			}else{
				html+='<td><div sprice="'+price+'" storage="'+storage+'" uid="'+uid+'" lprice="'+lprice+'" date="'+date+'" class="detail valid"><span>'+beginDate.getDate()+inputHtml+'</span><div class="price">'+priceT+'</br>'+lpriceT+'</br>'+storageT+'</div></div></td>';
			}
			if(i%7==6){
				html+='</tr>';
			}
			beginDate.setTime(beginDate.getTime()+24*3600000);
		}
		html+="</tbody></table></div>";
		return html;
	},
	show:function(yearmonth,pid,fromDate){
		var html;
		if(this.data[yearmonth]){
			html=this.outputHtml(this.data[yearmonth],yearmonth,fromDate);
		}else{
			$.ajax({
				url:"call/storage_Calendar_Ajax.php",
				async:false,
				type:"POST",
				dataType:"json",
				data:{"orderMonth":yearmonth,
						"pid":pid
				},
				success:function(json){
					html=storageCal.outputHtml(json,yearmonth,fromDate);
					storageCal.data[yearmonth]=json;
				}
			})
		}
		storageCal.init.calDiv.innerHTML=html;
		storageCal.init.calDiv.style.display="block";
		$(".weekSele").click(function(){
			var indexNum=this.value;
			if(this.checked==true){
				$("#storageCalContent tbody tr").each(function(){
					$(this).find("td").eq(indexNum).find("input").attr("checked",true);
				})
			}else{
				$("#storageCalContent tbody tr").each(function(){
					$(this).find("td").eq(indexNum).find("input").attr("checked",false);
				})
			}
		})
		$("#storageCalContent #checkAll").click(function(){
			if(this.checked==true){
				$("#storageCalContent input[type='checkbox']").each(function(){
					this.checked=true;
				})
			}else{
				$("#storageCalContent input[type='checkbox']").each(function(){
					this.checked=false;
				})
			}
		})
		/* $("#storageCalContent span.close").click(function(){
			storageCal.init.calDivClose();
		}) */
		var time=yearmonth+"-01";
			time=time.split("-");
			time=time.join("/");
			time=new Date(time);
		var lasttime=new Date(time.getFullYear(),time.getMonth()-1,1);
		var nexttime=new Date(time.getFullYear(),time.getMonth()+1,1);
		var lastmonth=lasttime.getFullYear()+"-"+this.strpad(lasttime.getMonth()+1);
		var nextmonth=nexttime.getFullYear()+"-"+this.strpad(nexttime.getMonth()+1);
		$("#storageCalContent .lastmonth").click(function(){
			storageCal.show(lastmonth,pid,fromDate);
		})
		$("#storageCalContent .nextmonth").click(function(){
			storageCal.show(nextmonth,pid,fromDate);
		})
	}
}; 